var web, name, sex, phone, email;
var raw;
// only for 智联
function raw_to_all(objDoc) {
	web = "智联";
	objTable = objDoc.getElementsByTagName("table")[3];
	name = objTable.getElementsByTagName("span")[0].innerHTML;
	console.log(name);

	str = objTable.rows[1].cells[1].innerHTML;
	str = str.replace(/[\s+\t+]+/ig,"");
	console.log(str);
	// phone
	phone_end = str.indexOf("(手机)");
	phone_start = phone_end - 11;
	phone = str.substring(phone_start, phone_end);
	console.log(phone);
	// email
	email_start = str.indexOf("mailto:") + 7;
	strEmail = str.substring(email_start);
	email_end = strEmail.indexOf("\"");
	email = strEmail.substring(0,  email_end);
	console.log(email);
	

/**
 * old version
	// user table
	raw = raw.substring(raw.indexOf("span style=\"font-size:40px;font-weight:bold;color:#000000;\""));
	
	raw = raw.substring(raw.indexOf("#000000"));
	raw = raw.substring(0, raw.indexOf("</a>"));

	// **正则替换掉其中的空格和制表符**
	raw = raw.replace(/[\s+\t+]+/ig,"");
	raw = raw.replace(/<br>/ig,"|");
//	console.log(raw);

	// #000000;">
	// name
	raw = raw.substring(raw.indexOf("#000000") + 10);
	name = raw.substring(0, raw.indexOf("<"));
	console.log(name);

	// offset to sex
	// 男|已婚|1983年11月生|户口：安徽池州|现居住于北京-丰台区|本科|8年工作经验|群众北京市丰台区长辛店芦井29号100000|13811125621(手机)|E-mail:<ahref=|mailto:zhangjianaifeng@163.com|target=|_blank||zhangjianaifeng@163.com 
	// 女|未婚|1984年1月生|户口：河北廊坊|现居住于北京|大专|8年工作经验|团员<spanstyle=|display:none;|||</span|18611101870(手机)|E-mail:<ahref=|mailto:guoaihuamm@163.com|target=|_blank||guoaihuamm@163.com )
	raw = raw.substring(raw.indexOf("right\">") + 7);
	raw = raw.replace(/\"/ig,"|");
	raw = raw.replace(/>/ig,"|");
//	console.log(raw);
	var arr = raw.split("|");
	for (i in arr) 
		;
//		console.log(arr[i]);

	sex = arr[0];
	console.log(sex);
	phone =  arr[i-6].substring(0, arr[i-6].indexOf("("));
	console.log(phone);
	email = arr[i];
	console.log(email);
*/
}

// for qiancheng
function process_qian_document(objDoc) {
	web = "前程";
	var idxTable = 9;
	var offsetPhoneEmail = 0;
	var strDoc = objDoc.body.innerHTML;

	console.log(strDoc.indexOf("简历关键字"));
	if (-1 != strDoc.indexOf("简历关键字"))
		idxTable = 10;
	if (-1 != strDoc.indexOf("地　址："))
		offsetPhoneEmail = 1;
	// name
	name = objDoc.getElementsByTagName("strong")[0].innerHTML;
	console.log(name);
	// phone
	objTable = objDoc.getElementsByTagName("table")[idxTable];
	strPhone = objTable.rows[2 + offsetPhoneEmail ].cells[1].innerHTML;
	phone = (strPhone.substring(0, strPhone.indexOf("（"))); //中文大括号
	console.log(phone);

	// e-mail
	strEmail = objTable.rows[3 + offsetPhoneEmail].cells[1].innerHTML;
	strEmail = strEmail.replace(/[\s+\t+]+/ig,"");
	console.log(strEmail);
	strEmail = strEmail.substring(strEmail.indexOf("blank\">") + 7);
	email = strEmail.substring(0, strEmail.indexOf("</a>"));
	console.log(email);
//	alert(strPhone.lastIndexOf("<")); Error in event handler for runtime.onMessage: Error: Attempting to use a disconnected port object
}

function get_iframe_to_str() {
	// get str from input src
	var str=((document.getElementsByTagName("input")[2]).value);

	str = str.substring(str.indexOf("1tbi"));
	str = str.substring(0 + 4, str.indexOf("%"));
	for (var i = 0; i < document.getElementsByTagName("iframe").length; i++) {
		if ( -1 != (document.getElementsByTagName("iframe")[i].src).indexOf(str))
			break;
	}

	// copy co raw
	raw = (document.getElementsByTagName("iframe")[i].contentDocument.body.innerHTML);
	var objDoc = (document.getElementsByTagName("iframe")[i].contentDocument);
//	console.log(raw);
	if (-1 != raw.indexOf("智联招聘")) {
		console.log("智联：" + raw.indexOf("智联招聘"));
		raw_to_all(objDoc);
	} else if ( -1 != raw.ind) {
		console.log("前程");
		process_qian_document(objDoc);
	}
}


chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
  console.log(response.tips);
});

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello"){
    	console.log("I'm contentscript,goodbye!");
	get_iframe_to_str();
//	raw_to_all();
	sendResponse({farewell: "I'm contentscript,goodbye!", cweb: web, cname: name, csex: sex, cphone: phone, cemail: email});
    }
  });
